package com.nlecloud.bc.model;

import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * @author warrior
 * @description 分页对象
 */
@Data
public class PageResult<T> implements Serializable {
    private static final long serialVersionUID = -6360280645542607446L;
    //当前页
    private Integer pageNum;
    //每页的数量
    private Integer pageSize;
    //总共的条数
    private Long total;
    //总共的页数
    private Integer pages;
    //实体类集合
    private List<T> list;

    /**
     * MongDb分页公共方法
     * @param pageSize 每页的数量
     * @param pageNum 当前的页数
     * @param total 总计数
     * @param list 对象集合
     * @return
     */
    public static PageResult pagination(int pageSize, int pageNum,long total,List list ) {
        Integer pages = (int)Math.ceil((double)total / (double)pageSize);
        if (pageNum <= 0 || pageNum > pages) {
            pageNum = 1;
        }
        int skip = pageSize * (pageNum - 1);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setPages(pages);
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setList(list);
        return pageResult;
    }

    public static PageResult emptyPageResult(int pageSize, int pageNum){
        PageResult pageResult = new PageResult();
        pageResult.setTotal(0L);
        pageResult.setPages(0);
        pageResult.setPageSize(pageSize);
        pageResult.setPageNum(pageNum);
        pageResult.setList(Collections.emptyList());
        return pageResult;
    }

}
